package org.grayrabbit.cms.publish.generate.factory;

import java.util.List;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.api.Publishable;
import org.grayrabbit.cms.publish.generate.CatMission;
import org.grayrabbit.cms.publish.generate.ContentMission;
import org.grayrabbit.cms.publish.generate.Mission;
import org.grayrabbit.cms.publish.generate.SiteMission;
import org.grayrabbit.cms.publish.generate.SurveryMission;
import org.grayrabbit.cms.publish.generate.TopicMission;

public class MissionFactory{
	
	private Generatable generatable;
	public MissionFactory(Generatable generatable){
		this.generatable = generatable;
	}
	/**
	 * 生成工作任务
	 * @param genType 生成范围类型 【"site":"站点","cat":"栏目","topic":"专题","":""】
	 * @param modelType 生成模式 【"1":"全量","2":"增量","3":"首页","0":"首页与列表页"】
	 * @param ip		客户端IP
	 * @param targets	生成对象ID集合
	 * @param site	生产目标站点
	 * @param admin	当前操作管理员
	 * @return
	 */
	public Mission create(String genType,String modelType,String ip,List<String> targets,Site  site,Admin admin){
		if(Publishable.SITE_GEN.equals(genType)){
			SiteMission siteMission = new SiteMission();
			siteMission.setModelType(modelType);
			siteMission.setList(targets);
			siteMission.setGenType(genType);
			siteMission.setAdmin(admin);
			if(Publishable.ALL_PUBLISH.equals(modelType)){
				//1 全量生成
				siteMission.setIndexNum(targets.size());
				siteMission.setCatNum(generatable.countCatBySites(targets) * 2);
				siteMission.setTopicNum(generatable.countTopicBySites(targets) * 2);
				siteMission.setContentNum(generatable.countContentBySites(targets));
				siteMission.setSurveryNum(generatable.countSurveyBySites(targets));
			}else if(Publishable.MORE_PUBLISH.equals(modelType)){
				//增量生成
				siteMission.setIndexNum(targets.size());
				siteMission.setCatNum(generatable.countMoreCatBySites(targets) * 2);
				siteMission.setTopicNum(generatable.countMoreTopicBySites(targets) * 2);
				siteMission.setContentNum(generatable.countMoreContentBySites(targets));
				siteMission.setSurveryNum(generatable.countSurveyBySites(targets));
			}else if(Publishable.INDEX_PUBLISH.equals(modelType)){
				//发布首页
				siteMission.setIndexNum(targets.size());
			}
			else if(Publishable.LIST_AND_INDEX_PUBLISH.equals(modelType)){
				//1 只发布站点栏目及首页,调查
				siteMission.setIndexNum(targets.size());
				siteMission.setCatNum(generatable.countCatBySites(targets) * 2);
				siteMission.setTopicNum(generatable.countTopicBySites(targets) * 2);
				siteMission.setSurveryNum(generatable.countSurveyBySites(targets));
			}
			
			return siteMission;
		}
		else if(Publishable.CATEGORY_GEN.equals(genType)){
			CatMission catMission = new CatMission();
			catMission.setModelType(modelType);
			catMission.setGenType(genType);
			catMission.setList(targets);
			catMission.setAdmin(admin);
			if(Publishable.ALL_PUBLISH.equals(modelType)){
				//栏目全量
					catMission.setCatNum(targets.size()*2);
					catMission.setContentNum(generatable.countContentByCats(targets));
			}else if(Publishable.MORE_PUBLISH.equals(modelType)){
				//增量
				catMission.setCatNum(targets.size()*2);
				catMission.setContentNum(generatable.countMoreContentByCats(targets));
			}else if(Publishable.INDEX_PUBLISH.equals(modelType)){
				//首页
				catMission.setCatNum(targets.size()*2);
			}else if(Publishable.LIST_PUBLISH.equals(modelType)){
				//列表
				catMission.setCatNum(targets.size()*2);
			}
			return catMission;
		}else if(Publishable.TOPIC_GEN.equals(genType)){
			TopicMission topicMission = new TopicMission();
			topicMission.setModelType(modelType);
			topicMission.setGenType(genType);
			topicMission.setList(targets);
			topicMission.setAdmin(admin);
			if(Publishable.ALL_PUBLISH.equals(modelType)){
				//全量
				topicMission.setTopicNum(targets.size()*2);
				topicMission.setContentNum(generatable.countContentByTopics(targets));
			}else if(Publishable.MORE_PUBLISH.equals(modelType)){
				//增量
				topicMission.setTopicNum(targets.size()*2);
				topicMission.setContentNum(generatable.countMoreContentByTopics(targets));
			}else if(Publishable.INDEX_PUBLISH.equals(modelType)){
				//首页
				topicMission.setTopicNum(targets.size()*2);
			}else if(Publishable.LIST_PUBLISH.equals(modelType)){
				//列表
				topicMission.setTopicNum(targets.size()*2);
			}
			return topicMission;
		}else if(Publishable.SURVEY_GEN.equals(genType)){
			SurveryMission surveryMission = new SurveryMission();
			surveryMission.setModelType(modelType);
			surveryMission.setSurveryNum(targets.size());
			surveryMission.setGenType(genType);
			surveryMission.setList(targets);
			surveryMission.setAdmin(admin);
			return surveryMission;
		}else if(Publishable.CONTENT_GEN.equals(genType)){
			ContentMission contentMission = new ContentMission();
			contentMission.setModelType(modelType);
			contentMission.setSurveryNum(targets.size());
			contentMission.setGenType(genType);
			contentMission.setList(targets);
			contentMission.setAdmin(admin);
			return contentMission;
		}
		return null;
		
	}
}